-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor TimePoint getters and fix decimal quantity dump rounding #187
Conversation
Fix rounding bug for decimal time quantities
TimePoints are immutable now, so we don't need to worry about testing for mutating an attr to a bad value
@@ -880,11 +880,11 @@ class TimePoint: | |||
|
|||
Keyword arguments (usually default to None if not provided): | |||
|
|||
expanded_year_digits (default 0) - an agreed-upon number of extra | |||
digits to represent the year, beyond the default of 4. For example, | |||
num_expanded_year_digits (default 0) - an agreed-upon number of extra |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a preference for expanded_year_digits
or num_expanded_year_digits
, but as this property is changing for TimePoint
, should we change it in the rest of the code too? grep
'ing for expanded.year.digit
, looks like test__00.py
, parser_spec.py
, data.py
, parsers.py
, dumpers.py
, and README.md
mention expanded year digits.
Are those reference OK, in which case this rename here is necessary as this property doesn't represent TimePoint's expanded year digits? (not sure if that makes sense, sorry 😆 ), or are we going to rename them too later?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the difference between the number of digits and the actual digits themselves
isodatetime/metomi/isodatetime/data.py
Lines 1110 to 1111 in 2b908ad
@property | |
def num_expanded_year_digits(self): return self._num_expanded_year_digits |
isodatetime/metomi/isodatetime/data.py
Lines 1233 to 1237 in 2b908ad
@property | |
def expanded_year_digits(self): | |
"""The extra digits at the front of the expanded year, as opposed to | |
the number of such digits""" | |
return abs(self._year / 10000) |
(not sure why those two are showing different indentations, they're the same)
Before, we had TimePoint.expanded_year_digits
which was the number of digits, and TimePoint.get("expanded_year_digits")
which was the digits themselves. The former should probably have always been called num_expanded_year_digits
, whereas the latter has not been renamed (just converted to @property
)
…ound up to 1 This is to prevent e.g. minute_of_hour dumping "60.0". Tests added
Unless truncated TimePoint. Tests included.
2b908ad
to
3798759
Compare
(The tests on Travis have passed, it's just Codacy complaining about an unassigned variable or something in a test) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks fine, adds more breaking changes for Cylc to deal with but they are pretty small and easy to handle.
metomi/isodatetime/data.py
Outdated
# Truncate instead of rounding up because ticking over the higher | ||
# quantities would be complicated | ||
return "999999" | ||
string = "%f" % decimal |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps make it clearer that the rounding is happening implicitly in a string formatting expression (I had to look it up):
string = "%f" % decimal | |
string = "%0.6f" % decimal |
Or just use round(decimal, 6)
?
value_error_timepoint = data.TimePoint(minute_of_hour=10) | ||
value_error_timepoint._minute_of_hour = "1O" | ||
self.assertRaises(ValueError, dumper.dump, value_error_timepoint, "%M") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Behaviour change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that TimePoints are "immutable", we shouldn't have to worry about mutating a property to an invalid value
BTW to make codacy happy just dedent that function and remove the unused |
Oh, codacy only shows the newest problem it has found with the code. Last time it was failing because of this expression-not-assigned (W0106). Trouble is, if you assign it, flake8 will flag it as an unused variable! |
@kinow just a reminder this is awaiting your review, no rush though |
This branch was kicking around for a while; I probably meant to create a PR earlier.
@property
decorators for getter methods, instead of having a mix of those and aget(self, property_name)
method59.999999999
-->60.0
. Just truncate the quantity instead (in this specific circumstance), so it dumps as59.999999
to 6 d.p.